package com.example.a2023110445

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.example.a2023110445.model.Player

class MainActivity : AppCompatActivity() {
    
    private lateinit var tvName: TextView
    private lateinit var tvGender: TextView
    private lateinit var tvPhone: TextView
    private lateinit var tvHighScore: TextView
    private lateinit var btnEditInfo: Button
    private lateinit var btnStartGame: Button
    
    private var player = Player()
    
    companion object {
        const val REQUEST_CODE_EDIT_INFO = 1001
        const val REQUEST_CODE_GAME = 1002
        const val EXTRA_PLAYER = "player"
        const val EXTRA_NEW_SCORE = "new_score"
    }
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        initViews()
        updatePlayerInfo()
        
        btnEditInfo.setOnClickListener {
            val intent = Intent(this, EditInfoActivity::class.java)
            intent.putExtra(EXTRA_PLAYER, player)
            startActivityForResult(intent, REQUEST_CODE_EDIT_INFO)
        }
        
        btnStartGame.setOnClickListener {
            val intent = Intent(this, CardGameActivity::class.java)
            intent.putExtra(EXTRA_PLAYER, player)
            startActivityForResult(intent, REQUEST_CODE_GAME)
        }
    }
    
    private fun initViews() {
        tvName = findViewById(R.id.tvName)
        tvGender = findViewById(R.id.tvGender)
        tvPhone = findViewById(R.id.tvPhone)
        tvHighScore = findViewById(R.id.tvHighScore)
        btnEditInfo = findViewById(R.id.btnEditInfo)
        btnStartGame = findViewById(R.id.btnStartGame)
    }
    
    private fun updatePlayerInfo() {
        val nameText = getString(R.string.player_name) + 
                (if (player.name.isEmpty()) getString(R.string.default_name) else player.name)
        val genderText = getString(R.string.player_gender) + 
                (if (player.gender.isEmpty()) getString(R.string.default_gender) else player.gender)
        val phoneText = getString(R.string.player_phone) + 
                (if (player.phone.isEmpty()) getString(R.string.default_phone) else player.phone)
        val scoreText = getString(R.string.player_high_score) + player.highScore.toString()
        
        tvName.text = nameText
        tvGender.text = genderText
        tvPhone.text = phoneText
        tvHighScore.text = scoreText
    }
    
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        
        when (requestCode) {
            REQUEST_CODE_EDIT_INFO -> {
                if (resultCode == RESULT_OK && data != null) {
                    player = data.getParcelableExtra(EXTRA_PLAYER) ?: Player()
                    updatePlayerInfo()
                }
            }
            REQUEST_CODE_GAME -> {
                if (resultCode == RESULT_OK && data != null) {
                    val newScore = data.getIntExtra(EXTRA_NEW_SCORE, 0)
                    if (newScore > player.highScore) {
                        player.highScore = newScore
                        updatePlayerInfo()
                    }
                }
            }
        }
    }
}
